package com.newdjk.newdoctor.ui;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatButton;

import com.newdjk.newdoctor.BasActivity;
import com.newdjk.newdoctor.R;
import com.newdjk.newdoctor.net.BaseComPleteObserver;
import com.newdjk.newdoctor.net.NetServices;
import com.newdjk.newdoctor.utils.Contants;
import com.newdjk.newdoctor.utils.LogUtils;
import com.newdjk.newdoctor.utils.MyCountDownTimer;
import com.newdjk.newdoctor.utils.SpUtils;
import com.newdjk.newdoctor.utils.StrUtil;
import com.newdjk.newdoctor.utils.ToastUtil;
import com.newdjk.okhttp.entity.BaseEntity;
import com.newdjk.okhttp.entity.VerfyCodeEntity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 忘记密码
 */
public class ChangePasswordActivity extends BasActivity {


    private final static String TAG = "forgetPass";
    @BindView(R.id.input_user)
    EditText inputUser;
    @BindView(R.id.input_bd_code)
    EditText inputBdCode;
    @BindView(R.id.tv_bd_code)
    TextView tvBdCode;
    @BindView(R.id.relate_code_bd)
    RelativeLayout relateCodeBd;
    @BindView(R.id.btn_submit)
    AppCompatButton btnSubmit;
    private MyCountDownTimer mcdt;

    public static Intent getIntent(Context context) {
        return new Intent(context, ChangePasswordActivity.class);
    }

    @Override
    public boolean IsShowBackIcon() {
        return true;
    }

    @Override
    protected int initViewResId() {
        return R.layout.activity_changepassword;
    }

    @Override
    protected void initView() {
        mcdt = new MyCountDownTimer(tvBdCode, 60000, 1000);
        String mobile= SpUtils.getString(Contants.userName);
        inputUser.setText(mobile);
    }

    @Override
    protected void initListener() {
        tvBdCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestCode();
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBtnSubmitClicked();
            }
        });
    }

    @Override
    protected void initData() {

    }


    /**
     * 发送验证码请求
     */
    public void requestCode() {
        if (!StrUtil.isMobile(StrUtil.getString(inputUser))) {
            ToastUtil.setToast("手机号码格式不正确");
        } else {
            NetServices.Factory.getService().SendMobileCode(StrUtil.getString(inputUser)
            ).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseComPleteObserver(ChangePasswordActivity.this) {
                        @Override
                        protected void onSuccees(BaseEntity t) throws Exception {
                            LogUtils.d(TAG, t.toString());
                            if (t.getCode() == 0) {
                                mcdt.start();
                            }
                            ToastUtil.setToast("发送成功");
                        }
                    });
        }
    }

    /**
     * 下一步
     */
    public void onBtnSubmitClicked() {


        if (!StrUtil.isMobile(StrUtil.getString(inputUser))) {
            ToastUtil.setToast("手机号码格式不正确");
            return;
        } else if (!StrUtil.isNotEmpty(inputBdCode, true)) {
            ToastUtil.setToast("验证码不能为空");
            return;
        } else if (inputBdCode.getText().toString().trim().length() != 4) {
            ToastUtil.setToast("验证码不对");
        } else {
            NetServices.Factory.getService().QueryDoctorByMobileCode(StrUtil.getString(inputUser), StrUtil.getString(inputBdCode)
            ).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseComPleteObserver<VerfyCodeEntity>(ChangePasswordActivity.this) {
                        @Override
                        protected void onSuccees(BaseEntity<VerfyCodeEntity> t) throws Exception {
                            LogUtils.d(TAG, t.toString());
                            Intent changepassword = new Intent(ChangePasswordActivity.this, ResettingActivity.class);
                            changepassword.putExtra("userId",t.getData().getDrId());
                            startActivity(changepassword);

                        }
                    });
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mcdt != null) {
            mcdt.cancel();
        }

    }


    @Override
    protected String setTitle() {
        return "忘记密码";
    }
}
